package com.bookSystem.servlet;/*
 * @Author:lsx
 * @Date:2024/9/15--09--15--22:08
 *
 */

import com.bookSystem.entity.Book;
import com.bookSystem.entity.User;
import com.bookSystem.service.Impl.BookServiceImpl;
import com.bookSystem.service.Impl.UserServiceImpl;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.List;

@WebServlet("/userServlet")
public class UserServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        UserServiceImpl userService = new UserServiceImpl();
        BookServiceImpl bookService = new BookServiceImpl();
        String account = request.getParameter("account");
        String password =request.getParameter("password");
        int login = userService.login(account, password);
        if (userService.login(account,password)==1 && userService.selectByAccountAndUserPass(account,password)!=null){
            User user=userService.selectByAccountAndUserPass(account,password);
            request.setAttribute("user",user);
            List<Book> books = bookService.selectAll();
            request.setAttribute("books",books);
            request.getRequestDispatcher("index.jsp").forward(request,response);
        }else{
            request.setAttribute("msg","密码错误或用户名不存在");
            request.getRequestDispatcher("login.jsp").forward(request,response);
        }

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
